package sfsoftware.game.actor.enemy;

import sfsoftware.game.actor.attributes.Collidable;
import sfsoftware.game.trevor.Enemy;
import sfsoftware.game.trevor.Player;
import sfsoftware.util.Actor;
import sfsoftware.util.Tile;
import sfsoftware.util.Updatable;

public class Bullet extends Enemy implements Updatable {
	float redValue = 1;
	boolean redShiftUp = false;
	int damage = 10;
	
	public Bullet(int destX, int destY, String texture, int destWidth, int destHeight, Tile[][] assocTileMap) {
		super(destX, destY, texture, destWidth, destHeight);
		setSpeed(4);
		tileMap = assocTileMap;
		direction = (int)(Math.random() * 3);
	}

	public Bullet(int destX, int destY, String texture, int destWidth, int destHeight, Tile[][] assocTileMap, int bulletDamage) {
		super(destX, destY, texture, destWidth, destHeight);
		setSpeed(4);
		tileMap = assocTileMap;
		direction = (int)(Math.random() * 3);
		damage = bulletDamage;
	}
	
	public Bullet(int destX, int destY, String texture, int srcX, int srcY,	int destWidth, int destHeight, Tile[][] assocTileMap) {
		super(destX, destY, texture, srcX, srcY, destWidth, destHeight);
		setSpeed(4);
		tileMap = assocTileMap;
		direction = (int)(Math.random() * 3);
	}
	
	public Bullet(int destX, int destY, String texture, int srcX, int srcY,	int destWidth, int destHeight, Tile[][] assocTileMap, int bulletDamage) {
		super(destX, destY, texture, srcX, srcY, destWidth, destHeight);
		setSpeed(4);
		tileMap = assocTileMap;
		direction = (int)(Math.random() * 3);
		damage = bulletDamage;
	}
	
	@Override
	public void collisionResult(Collidable object) {
		// Player should take damage here.
		Player.hurt(damage);
		//generateParticles(32, 0.75f, 0, 0);
		//Actor.cast.remove(this);		
	}

	@Override
	public void draw()
	{
		if(redShiftUp)
		{
			redValue = redValue + 0.05f;
			if(redValue >= 1) {
				redValue = 1;
				redShiftUp = false;
			}
		}
		else
		{
			redValue = redValue - 0.05f;
			if(redValue <= 0.5f) {
				redValue = 0.5f;
				redShiftUp = true;
			}
		}
		super.draw(width, height, this.getSrcX(), this.getSrcY(), width, height, redValue, 0, 0, 1);
	}

	@Override
	public void explode(Actor a) {
		// Bullets cannot explode, so we need an override of the Enemy explode methods! ;)
	}

	@Override
	public void explode() {
		// Bullets cannot explode, so we need an override of the Enemy explode methods! ;)
	}	
	
	@Override
	public boolean move(double dirX, double dirY, Tile[][] tileMap)
	{		
		boolean successfulMove = false;
		try
		{
			// Try to move!
			successfulMove = super.move(dirX, dirY, tileMap); 
		}
		catch(ArrayIndexOutOfBoundsException e)
		{
			
		}
		return successfulMove;
	}
	
	@Override
	public void update() {
		int xDirection = 0;
		int yDirection = 0;
		switch(direction)
		{
			case 0:
				yDirection = 1;
				break;
			case 1:
				yDirection = -1;
				break;
			case 2:				
				xDirection = 1;
				break;
			case 3:
				xDirection = -1;
				break;				
		}
		
		if(!(move(xDirection, yDirection, tileMap)))
		{
			generateParticles(40, 0.75f, 0, 0);
			Actor.cast.remove(this);
		}
	}
	
	@Override
	public void wiffaHit() 
	{
	
	}

}
